<?php 
//this file handles all the jazz for forum admin

function addGroup($name, $description){
	global $link;
	
	//clean up the incoming vars
	$name = mysql_real_escape_string($name);
	$description = mysql_real_escape_string($description);
	
	//insert the group
	$result = mysql_query("INSERT INTO ".TABLE_PREFIX."_groups 
						   (name, description) 
						   VALUES 
						   ('".$name."', '".$description."')");
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//if all was good then we need to put all the global mods and admins into the members of the new group
	$newGroupID = mysql_insert_id();
	$result = mysql_query("SELECT DISTINCT userID, isAdmin, isModerator, isAllowedToPost
						   FROM ".TABLE_PREFIX."_groupmembers 
						   WHERE groupID = 3
						   OR groupID = 4");
	
	//loop through the mods/admins and give them access to the new group	
	while($data = mysql_fetch_object($result)){
		//insert them into the members based on thier current perms
		$insert = mysql_query("INSERT INTO ".TABLE_PREFIX."_groupmembers 
							   (userID, groupID, isAdmin, isModerator, isAllowedToPost)
							   VALUES 
							   (".$data->userID.", ".$newGroupID.", ".$data->isAdmin.", ".$data->isModerator.", ".$data->isAllowedToPost.")");
		if(!$result){
			echo "output=mySqlError&error=".mysql_error();
			return;
		}
	}	
	
	//report success
	return "output=success";
}

function saveGroup($id, $name, $description){
	global $link;
	
	//clean up the incoming vars
	$id = mysql_real_escape_string($id);
	$name = mysql_real_escape_string($name);
	$description = mysql_real_escape_string($description);
	
	//update the group
	$result = mysql_query("UPDATE ".TABLE_PREFIX."_groups SET name = '".$name."', description = '".$description."' WHERE groupID = ".$id);
	
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//all good
	return "output=success";
}

function deleteGroup($id){
	global $link;
	
	//clean up the incoming vars
	$id = mysql_real_escape_string($id);
	
	//delete the group
	$result = mysql_query("DELETE FROM ".TABLE_PREFIX."_groups WHERE groupID = ".$id);
	
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//all good
	return "output=success";
}

function getUsers($id){
	global $link;
	
	//clean up the incoming vars
	$id = mysql_real_escape_string($id);
	
	//get the users that are part of the group
	$result = mysql_query("SELECT u.userID, u.username, m.isAdmin, m.isModerator
							FROM ".TABLE_PREFIX."_groupmembers m, ".TABLE_PREFIX."_users u
							WHERE m.groupID = ".$id." 
							AND m.userID = u.userID
							ORDER BY u.username ASC");
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	$totalUsers = mysql_num_rows($result);
	
	//output the data
	$output = "ouput=success";
	$output .= "&totalUsers=".$totalUsers;
	
	//add all the users to the output
	for($i = 0; $i < $totalUsers; $i++){
		$data = mysql_fetch_object($result);
		$output .= "&user".$i."ID=".$data->userID;
		$output .= "&user".$i."Name=".stripslashes(urlencode($data->username));
		$output .= "&user".$i."isAdmin=".$data->isAdmin;
		$output .= "&user".$i."isModerator=".$data->isModerator;
	}
	
	return $output;
}
?>